package com.zlz.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlz.server.mapper.DepartmentMapper;
import com.zlz.server.pojo.Department;
import com.zlz.server.pojo.RespBean;
import com.zlz.server.service.IDepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author efire
 * @since 2021-01-27
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }

    @Override
    public RespBean addDept(Department department) {
        department.setEnabled(true);
        departmentMapper.addDept(department);
        if (department.getResult() == 1) {
            return RespBean.success("添加成功", department);
        }
        return RespBean.error("添加失败");
    }

    @Override
    public RespBean delDepartmentById(Long id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.delDepartmentById(department);
        if (department.getResult() == 1) {
            return RespBean.success("删除成功", null);
        }
        if (department.getResult() == -1) {
            return RespBean.error("该部门下还有员工，删除失败");
        }
        if (department.getResult() == -2) {
            return RespBean.error("该部门下还是子部门，删除失败");
        }
        return RespBean.error("删除失败");
    }
}
